package com.javaDemo;

import com.alibaba.excel.write.handler.CellWriteHandler;
import com.javaDemo.excel.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * PQRR模型配置表
 * </p>
 *
 * @author zc
 * @since 1.0.0
 * @date 2025-04-16
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @GetMapping("/exportInfos")
    public void exportInfos(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ClassPathResource resource = new ClassPathResource("template/demo.xlsx");
        InputStream inputStream = resource.getInputStream();
        ServletOutputStream outputStream = response.getOutputStream();
        HashMap<String, Object> map = new HashMap<>();
        List<SpotInspectionExportExcelModel> list = new ArrayList<>();
        SpotInspectionExportExcelModel spotInspectionImportExcelModel = new SpotInspectionExportExcelModel();
        spotInspectionImportExcelModel.setNum("1");
        spotInspectionImportExcelModel.setDevelopmentDimension("项目开发");
        spotInspectionImportExcelModel.setWorkItem("ESOW");
        spotInspectionImportExcelModel.setEvaluationRule("ESOW的技术要求、实验标准、三包等要求是否与客户达成一致？");
        spotInspectionImportExcelModel.setDataFlag("否");
        spotInspectionImportExcelModel.setDiscription("1");
        spotInspectionImportExcelModel.setRisk("Y");
        spotInspectionImportExcelModel.setCountermeasure("11");
        list.add(spotInspectionImportExcelModel);
        SpotInspectionExportExcelModel spotInspectionImportExcelModel1 = new SpotInspectionExportExcelModel();
        spotInspectionImportExcelModel1.setNum("2");
        spotInspectionImportExcelModel1.setDevelopmentDimension("项目开发");
        spotInspectionImportExcelModel1.setWorkItem("ESOW");
        spotInspectionImportExcelModel1.setEvaluationRule("关键供应商开发计划满足项目要求？");
        spotInspectionImportExcelModel1.setDataFlag("是");
        spotInspectionImportExcelModel1.setDiscription("2");
        spotInspectionImportExcelModel1.setRisk("R");
        spotInspectionImportExcelModel1.setCountermeasure("22");
        list.add(spotInspectionImportExcelModel1);
        SpotInspectionExportExcelModel spotInspectionImportExcelModel2 = new SpotInspectionExportExcelModel();
        spotInspectionImportExcelModel2.setNum("3");
        spotInspectionImportExcelModel2.setDevelopmentDimension("项目开发");
        spotInspectionImportExcelModel2.setWorkItem("质量目标");
        spotInspectionImportExcelModel2.setEvaluationRule("是否与客户签署质量协议及质量目标？");
        spotInspectionImportExcelModel2.setDataFlag("否");
        spotInspectionImportExcelModel2.setDiscription("3");
        spotInspectionImportExcelModel2.setRisk("G");
        spotInspectionImportExcelModel2.setCountermeasure("33");
        list.add(spotInspectionImportExcelModel2);
        SpotInspectionExportExcelModel spotInspectionImportExcelModel3 = new SpotInspectionExportExcelModel();
        spotInspectionImportExcelModel3.setNum("4");
        spotInspectionImportExcelModel3.setDevelopmentDimension("质量目标");
        spotInspectionImportExcelModel3.setWorkItem("供应商管理");
        spotInspectionImportExcelModel3.setEvaluationRule("关键供应商零部件是否完成PPAP审核及PSW签署？");
        spotInspectionImportExcelModel3.setDataFlag("是");
        spotInspectionImportExcelModel3.setDiscription("4");
        spotInspectionImportExcelModel3.setRisk("R");
        spotInspectionImportExcelModel3.setCountermeasure("44");
        list.add(spotInspectionImportExcelModel3);

        EasyExcelUtil.setExportFormat(response, request, "设备信息导出");
        // 设置第几行开始合并
        int mergeRowIndex = 6;
        // 需要合并的列
        int[] mergeColumnIndex = { 2, 3 };
        // Excel单元格行合并处理策略
        ExcelPQRRCellRowMergeStrategy rowMergeStrategy = new ExcelPQRRCellRowMergeStrategy(mergeRowIndex,
                mergeColumnIndex);
        // 列合并的工具实体类
        ArrayList<CellLineRange> cellLineRanges = new ArrayList<>();
        cellLineRanges.add(new CellLineRange(2, 3));
        // Excel单元格列合并处理策略
        ExcelColumnMergeStrategy celColumnMergeStrategy = new ExcelColumnMergeStrategy(6, cellLineRanges);
        // 添加风险颜色处理策略
        List<CellWriteHandler> cellWriteHandlerList = Stream
                .of(celColumnMergeStrategy, rowMergeStrategy).collect(Collectors.toList());
        map.put("demo1", "risk");
        map.put("demo2", "countermeasure");
        EasyExcelUtil.fillSinglePQRRExcelMerge(outputStream, inputStream, SpotInspectionExportExcelModel.class, list,
                map, "Sheet1", cellWriteHandlerList);
        outputStream.flush();
        outputStream.close();
    }

}
